package tv.youxing.android.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import tv.youxing.android.R;
import tv.youxing.android.custom.CustomGridView;
import tv.youxing.android.entity.DianZhanModel;
import tv.youxing.android.entity.DynamicDetailsPingLunModel;
import tv.youxing.android.entity.UserInfoModel;
import tv.youxing.android.ui.DynamicDetailsActivity;
import tv.youxing.android.ui.LoginActivity;
import tv.youxing.android.utils.AppConst;
import tv.youxing.android.utils.GsonUtils;
import tv.youxing.android.utils.PicassoUtils;
import tv.youxing.android.utils.ShareUtils;
import tv.youxing.android.utils.StaticClass;

/**
 * Created by apple on 2017/4/20.
 */

/**
 * 项目名：   youxing
 * 包名：     tv.youxing.android .adapter
 * 文件名：   DynamicAdapter
 * 创建者：   叶克武
 * 创建时间： 2017/4/20 上午11:02
 * 描述：    TODO
 */
public class UserInfoDetailsOneAdapter extends BaseAdapter {


    private Activity mActivity;
    private Context context;
    private List<UserInfoModel.ResultBean.DataBean> dynamicModelList = new ArrayList<>();
    private LayoutInflater layoutInflater;

    public UserInfoDetailsOneAdapter(Activity activity,Context context, List<UserInfoModel.ResultBean.DataBean> dynamicModelList) {
        this.mActivity = activity;
        this.dynamicModelList = dynamicModelList;
        this.context = context;
        this.layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    }



    @Override
    public int getCount() {
        return dynamicModelList.size();
    }

    @Override
    public Object getItem(int position) {
        return dynamicModelList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (viewHolder == null){
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.layout_item_dynamic,null);
            viewHolder.root = (LinearLayout) convertView.findViewById(R.id.root);
            viewHolder.ivAvatar = (CircleImageView) convertView.findViewById(R.id.ivIcon);
            viewHolder.tvName = (TextView) convertView.findViewById(R.id.tvName);
            viewHolder.tvContent = (TextView) convertView.findViewById(R.id.tvContent);
            viewHolder.gridView = (CustomGridView) convertView.findViewById(R.id.picGridView);
            viewHolder.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
            viewHolder.ivPraise = (ImageView) convertView.findViewById(R.id.ivPraise);
            viewHolder.tvPraise = (TextView) convertView.findViewById(R.id.tvPraise);
            viewHolder.tvComment = (TextView) convertView.findViewById(R.id.tvComment);
            viewHolder.llPinglun = (RelativeLayout) convertView.findViewById(R.id.pinglun);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }

        List<String> urlList = new ArrayList<>();

//        viewHolder.root.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Intent  intent  = new Intent(context, DynamicDetailsActivity_new.class);
//                intent.putExtra("resource_id",dynamicModelList.get(position).getTweet_id()+"");
//                context.startActivity(intent);
//            }
//
//        });

        //用户头像
        PicassoUtils.loadImageViewSize(context, null,60,60,R.drawable.me_def_avater,viewHolder.ivAvatar);


        viewHolder.ivAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent  intent  = new Intent(context, DynamicDetailsActivity_new.class);
//                intent.putExtra("user_id",dynamicModelList.get(position).getPublish_user().getUser_id()+"");
//                context.startActivity(intent);
//
            }
        });


        //名称
        viewHolder.tvName.setText(dynamicModelList.get(position).getPublish_user().getUsername());
        //发布内容
        viewHolder.tvContent.setText(dynamicModelList.get(position).getContent());

        urlList = dynamicModelList.get(position).getImages();
        //发布的图片
//        viewHolder.gridView.setAdapter(new ImageAdapter(mActivity,context, urlList));
//        viewHolder.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                L.i("viewHolder.gridView:  "+position);
//            }
//        });

        //时间
        viewHolder.tvTime.setText(dynamicModelList.get(position).getUpdated_at());

        //点赞
        viewHolder.ivPraise.setSelected(false);

        //点赞的点击事件
        final ViewHolder finalViewHolder = viewHolder;
        viewHolder.ivPraise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //post请求简洁版实现
                HttpParams params = new HttpParams();
                params.putHeaders("Authorization","Bearer "+ ShareUtils.getString(context, AppConst.TOKEN,""));
                params.put("resource_id",dynamicModelList.get(position).getTweet_id());
                params.put("resource_type",2);


                RxVolley.post(StaticClass.DianZan, params, new HttpCallback() {
                    @Override
                    public void onSuccess(String t) {
                        DianZhanModel dianZhanModel = GsonUtils.GsonToBean(t,DianZhanModel.class);

                        if (dianZhanModel.getCode() == AppConst.TOKENERROR) {
                            mActivity.startActivity(new Intent(mActivity,LoginActivity.class));
                            return;
                        }


                        if (dianZhanModel.getCode() == 200) {
                            finalViewHolder.ivPraise.setSelected(true);
                            int count = Integer.parseInt(dynamicModelList.get(position).getLike_count())+1;
                            finalViewHolder.tvPraise.setText(count+"");
                        }else{
                            finalViewHolder.ivPraise.setSelected(false);
                        }
                    }
                });
            }
        });

        //点赞数
        viewHolder.tvPraise.setText(dynamicModelList.get(position).getLike_count());

        //说说评论数
        viewHolder.tvComment.setText(dynamicModelList.get(position).getComment_count());

        //评论界面
        viewHolder.llPinglun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, DynamicDetailsActivity.class);

                Bundle bundle = new Bundle();


                DynamicDetailsPingLunModel model = new DynamicDetailsPingLunModel();
                DynamicDetailsPingLunModel.PublishUserBean model_user = new DynamicDetailsPingLunModel.PublishUserBean();

                model.setContent(dynamicModelList.get(position).getContent());
                model.setPublish_time(dynamicModelList.get(position).getPublish_time());
                model.setIs_like(dynamicModelList.get(position).getIs_like());
                model.setTweet_id(dynamicModelList.get(position).getTweet_id());
                model.setLike_count(dynamicModelList.get(position).getLike_count());

                model_user.setAvatar(dynamicModelList.get(position).getPublish_user().getAvatar());
                model_user.setUsername(dynamicModelList.get(position).getPublish_user().getUsername());


                model.setPublish_user(model_user);


                bundle.putSerializable("data",model);


                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });

        return convertView;
    }



    class ViewHolder{
        LinearLayout root;
        private CircleImageView ivAvatar;
        private TextView tvName;
        private TextView tvContent;
        private CustomGridView gridView;
        private TextView tvTime;
        private ImageView ivPraise;
        private TextView tvPraise;
        private TextView tvComment;
        private RelativeLayout llPinglun;
    }

}
